/**
 * Project Name:lee
 * File Name:UserServiceImpl.java
 * Package Name:com.lee.service.impl.system
 * Date:2016年12月9日上午9:44:57
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
 */

package com.lee.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lee.common.utils.Common;
import com.lee.system.mapper.UserInfoMapper;
import com.lee.system.model.UserInfo;
import com.lee.system.service.UserService;

/**
 * ClassName:UserServiceImpl <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年12月9日 上午9:44:57 <br/>
 * 
 * @author dell
 * @version
 * @since JDK 1.6
 * @see
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Override
	public int count(UserInfo t) {
		return userInfoMapper.count(t);
	}

	@Override
	public int insert(UserInfo t) {
		int i = 0;
		String date = Common.fromDateH();
		t.setLocked("1");
		t.setCreatetime(date);
		i = userInfoMapper.insert(t);
		return i;
	}

	@Override
	public int update(UserInfo t) {
		return userInfoMapper.update(t);
	}

	@Override
	public int delete(int id, boolean flag) {
		int i = 0;
		if (flag) { // 真删除
			i = userInfoMapper.delete(id);
		}
		if (!flag) { // 假删除
			UserInfo userInfo = userInfoMapper.findById(id);
			userInfo.setDeletestatus(1);
			i = userInfoMapper.update(userInfo);
		}
		userInfoMapper.deleteAttr(id);
		return i;
	}

	@Override
	public UserInfo findById(int id) {

		return userInfoMapper.findById(id);
	}

	@Override
	public List<UserInfo> findByPid(int id) {

		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserInfo findByName(String name) {
		return userInfoMapper.findByName(name);
	}

	@Override
	public List<UserInfo> findUserPage(Map<String, Object> map) {
		List<UserInfo> userInfos = userInfoMapper.findUserPage(map);
		return userInfos;
	}

	/**
	 * 
	 * TODO 删除用户的所有权限
	 * 
	 * @see com.lee.system.service.UserService#deleteUserRes(int)
	 */
	@Override
	public int deleteUserRes(int id) {
		int i = userInfoMapper.deleteAttr(id);
		return i;
	}

	@Override
	public int insertUserRes(int userId, int resId) {
		int i = userInfoMapper.insertUserRes(userId, resId);
		return i;
	}

	@Override
	public Map<String, Object> addUserRes(String userId, String ids) {
		Map<String, Object> map = new HashMap<String, Object>();

		String[] id = ids.split(",");
		int count = 0;
		// 删除用户的所有权限
		this.deleteUserRes(Integer.parseInt(userId));
		// 重新绑定新的权限
		for (String string : id) {
			int j = this.insertUserRes(Integer.parseInt(userId), Integer.parseInt(string));
			if (j > 0) {
				count++;
			}
		}
		if (id.length == count) {
			map.put("flag", true);
		} else {
			map.put("flag", false);
		}
		return map;
	}

	@Override
	public List<UserInfo> findUserByCount(Map<String, Object> map) {
		
		return userInfoMapper.findUserByCount(map);
	}
	
}
